$(function () {
    var pblogNickName=Core.getCookie("tz-cms-username");
    var pblogQQ=Core.getCookie("tz-cms-qq");
    var pblogEmail=Core.getCookie("tz-cms-email");
    if(pblogNickName!=""){
        $("#user-name-content").show();
        $("#user-name").text(pblogNickName);
        $("#nickname").val(pblogNickName);
        $("#qq").val(pblogQQ);
        $("#email").val(pblogEmail);
    }else{
        $("#user-info").show();
    }
    $("#qq").blur(function(){
        var qq=$("#qq").val();
        var nickname=$("#nickname").val();
        if(nickname.length==0&&qq.length > 0){
            if(!isNaN(qq)){
                Core.getQqInfo(qq,function (data) {
                    $("#nickname").val(data.nickname);
                });
            }else{
                layer.msg("qq格式不正确！")
            }
        }
    });

    function init(pageNumber) {
        Core.postAjax("/blog/api/comments",{"sid":sid,"pageNumber": (pageNumber==null? 1 : pageNumber), "pageSize": 10},function (data) {
            var commentOne="";
            if(data.total==0){
                commentOne+='<div class="no-comment">暂无评论，快来占领宝座</div>';
                $("#comment-ul").append(commentOne);
            }else{
                $.each(data.records,function (index,value) {
                      commentOne += '<li class="comment rounded"><div class="thumb circle">'+
                                    '	<div class="inner"><img src="'+(value.avatar||"/assets/img/user-default.png")+'"  alt="avatar" width="70px" height="70px" /></div></div>'+
                                    '		<div class="details">'+
                                    '			<sapn class="name"><b>'+value.nickname+'</b></sapn>'+
                                    '		    <span class="date">'+value.createTime+'</span>'+
                                    '		    <p>'+value.content+'</p>'+
                                    '			<a href="javascript:void(0)" class="btn btn-default btn-sm reply" pid="'+value.id+'" reply-id="'+value.id+'" >回复</a></div>'+
                                    '</li>';
                    if(value.children!=null){
                        $.each(value.children,function (i,v) {
                            commentOne += '<li class="comment child rounded"><div class="thumb circle">'+
                                '	<div class="inner"><img src="'+(v.avatar||"/assets/img/user-default.png")+'"  alt="avatar"  width="60px" height="60px" /></div></div>'+
                                '		<div class="details">'+
                                '			<sapn class="name"><b>'+v.nickname+'</b></sapn >&nbsp;回复&nbsp;<sapn class="name"><b>'+v.replyName+'</b></sapn>'+
                                '		    <span class="date">'+v.createTime+'</span>'+
                                '		    <p>'+v.content+'</p>'+
                                '			<a href="javascript:void(0)" class="btn btn-default btn-sm reply" pid="'+value.id+'" reply-id="'+v.id+'">回复</a></div>'+
                                '</li>';
                        })
                    }
                })
                $("#comment-more").remove();
                if(data.hasNextPage){
                    commentOne+='<div id="comment-more" data-page="'+data.nextPage+'" class="comment-more">加载更多</div>'
                }
                $("#comment-ul").append(commentOne);
                /*加载更多*/
                $("#comment-more").click(function () {
                    init($(this).attr("data-page"));
                })
                /*link至评论*/
                $(".comment-link").click(function () {
                    var commentLinkId = $(this).attr("data-link");
                    $("html,body").animate({
                        scrollTop:$("#"+commentLinkId).offset().top-55},{duration: 300,easing: "swing"})
                })

                $(".reply").click(function () {
                    var pid=$(this).attr("pid");
                    var replyId=$(this).attr("reply-id");
                    if($("#reply-comment-form").length>0){
                        $replyForm=$("#reply-comment-form");
                        $("#reply-comment-form").remove();
                        $(this).parent().after($replyForm);
                        $("#reply-comment-form").show();
                        $(".reply[style='display: none;']").next().hide();
                        $(".reply[style='display: none;']").show();
                        $("#pid").val(pid);
                        $("#replyId").val(replyId);
                    }else{
                        var replyForm =
                            '<form id="reply-comment-form" class="comment-form">'+
                            '<input name="sid" type="hidden" value="'+sid+'"  />'+
                            '<input id="pid" name="pid" type="hidden" value="'+pid+'"  />'+
                            '<input id="replyId" name="replyId" type="hidden" value="'+replyId+'"  />'+
                            '<div class="row">'+
                            '<div class="column col-md-4" style="display:'+(pblogNickName==""?"block":"none")+'" >'+
                            '<div class="form-group">'+
                            '<input type="email" class="form-control" id="reply-email" name="email" value="'+pblogEmail+'" placeholder="邮箱地址" required="required">'+
                            '</div>'+
                            '</div>'+
                            '<div class="column col-md-4" style="display:'+(pblogNickName==""?"block":"none")+'" >'+
                            '<div class="form-group">'+
                            '<input type="text" class="form-control"  id="reply-qq" name="qq" value="'+pblogQQ+'" placeholder="QQ（用于展示头像和昵称）" required="required">'+
                            '</div>'+
                            '</div>'+
                            '<div class="column col-md-4" style="display:'+(pblogNickName==""?"block":"none")+'" >'+
                            '<div class="form-group">'+
                            '<input type="text" class="form-control" id="reply-nickname" name="nickname" value="'+pblogNickName+'"   placeholder="昵称（必填）" required="required">'+
                            '</div>'+
                            '</div>'+
                            '<div class="column col-md-12">'+
                            '<div class="form-group">'+
                            '<textarea name="content" id="reply-content" class="form-control" rows="4" placeholder="Your comment here..." required="required"></textarea>'+
                            '</div>'+
                            '</div>'+
                            '</div>'+
                            '<button type="button" id="submitReplyCommentBtn"  class="btn btn-default">发表评论</button>'+
                            '</form>';
                        $(this).parent().after(replyForm);
                        $("#reply-qq").blur(function(){
                            var qq=$("#reply-qq").val();
                            var nickname=$("#reply-nickname").val();
                            if(nickname.length==0&&qq.length > 0){
                                if(!isNaN(qq)){
                                    Core.getQqInfo(qq,function (data) {
                                        $("#reply-nickname").val(data.nickname);
                                    });
                                }else{
                                    layer.msg("qq格式不正确！")
                                }
                            }
                        });
                    }
                    $(this).hide();
                    $(this).next().show();
                    $("#submitReplyCommentBtn").on('click',function () {
                        if($("#reply-nickname").val()==""){
                            layer.msg("请输入昵称")
                            return;
                        }else if($("#reply-content").val()==""){
                            layer.msg("说点什么吧")
                            return;
                        }
                        Core.postAjax("/blog/api/comment/save",$("#reply-comment-form").serialize(),function (data) {
                            if(Core.getCookie("tz-cms-username")==""){
                                Core.setCookie("tz-cms-username",$("#reply-nickname").val(),30);
                                Core.setCookie("tz-cms-qq",$("#reply-qq").val(),30);
                                Core.setCookie("tz-cms-email",$("#reply-email").val(),30);
                            }
                            layer.msg(data.msg, {
                                offset: '30%',
                                time: 800
                            }, function () {
                                if(data.status==200){
                                    location.reload();
                                }
                            });
                        })
                    })
                })
                $(".cancel-reply").click(function () {
                    $("#reply-comment-form").hide();
                    $(this).hide();
                    $(this).prev().show();
                })
                $(".comment-support").click(function () {
                    $thisLove = $(this);
                    Core.postAjax("/blog/api/love",{"bizId":$(this).attr("biz-id"),"bizType":2},function (data) {
                        if(data.status==200){
                            $thisLove.text(parseInt($thisLove.text())+1);
                        }
                    });
                })
            }

        })
    }
    init(1);

    /*提交评论*/
    $("#submitCommentBtn").click(function () {
        if($("#nickname").val()==""){
            layer.msg("请输入昵称")
            return;
        }else if($("#content").val()==""){
            layer.msg("说点什么吧")
            return;
        }
        $("#comment-sid").val(sid);
        Core.postAjax("/blog/api/comment/save",$("#comment-form").serialize(),function (data) {
            layer.msg(data.msg, {
                offset: '30%',
                time: 800
            }, function () {
                if(data.status==200){
                    if(pblogNickName!=$("#nickname").val()||pblogQQ!=$("#qq").val()||pblogEmail!=$("#email").val()){
                        Core.setCookie("tz-cms-username",$("#nickname").val(),30);
                        Core.setCookie("tz-cms-qq",$("#qq").val(),30);
                        Core.setCookie("tz-cms-email",$("#email").val(),30);
                    }
                    location.reload();
                }
            });
        })
    })
    /*点击用户名*/
    $("#user-name").click(function () {
        if($("#user-info").hasClass("user-show")){
            $("#user-info").slideUp();
            $("#user-info").removeClass("user-show");
        }else{
            $("#user-info").slideDown();
            $("#user-info").addClass("user-show");
        }

    })
})

